package com.ruoyi.stuinfo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.stuinfo.domain.StuRewards;
import com.ruoyi.teacher.service.impl.TeacherServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stuinfo.domain.StudentActivity;
import com.ruoyi.stuinfo.service.IStudentActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 综测情况Controller
 *
 * @author zc
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/stuinfo/activity")
public class StudentActivityController extends BaseController
{
    @Autowired
    private IStudentActivityService studentActivityService;
    @Autowired
    private TeacherServiceImpl teacherServiceImpl;

    /**
     * 查询综测情况列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentActivity studentActivity)
    {
        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<StudentActivity> list = studentActivityService.selectStudentActivityList(studentActivity);
            return getDataTable(list);
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            startPage();
            List<StudentActivity> list = studentActivityService.selectStudentActivityList_handteacher(teacherId);
            return getDataTable(list);
        }
        else
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            startPage();
            List<StudentActivity> list = studentActivityService.selectStudentActivityList_Xiteacher(teacherId);
            return getDataTable(list);
        }
    }

    /**
     * 导出综测情况列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:activity:export')")
    @Log(title = "综测情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentActivity studentActivity)
    {
        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            List<StudentActivity> list = studentActivityService.selectStudentActivityList(studentActivity);
            ExcelUtil<StudentActivity> util = new ExcelUtil<StudentActivity>(StudentActivity.class);
            util.exportExcel(response, list, "综测情况数据");
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            List<StudentActivity> list = studentActivityService.selectStudentActivityList_handteacher(teacherId);
            ExcelUtil<StudentActivity> util = new ExcelUtil<StudentActivity>(StudentActivity.class);
            util.exportExcel(response, list, "综测情况数据");
        }
        else
        {
            //系主任
            teacherId = teacherId.replace("X", "");
            List<StudentActivity> list = studentActivityService.selectStudentActivityList_Xiteacher(teacherId);
            ExcelUtil<StudentActivity> util = new ExcelUtil<StudentActivity>(StudentActivity.class);
            util.exportExcel(response, list, "综测情况数据");
        }
    }

    /**
     * 获取综测情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentActivityService.selectStudentActivityById(id));
    }

    /**
     * 新增综测情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:activity:add')")
    @Log(title = "综测情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentActivity studentActivity)
    {
        return toAjax(studentActivityService.insertStudentActivity(studentActivity));
    }

    /**
     * 修改综测情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:activity:edit')")
    @Log(title = "综测情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentActivity studentActivity)
    {
        return toAjax(studentActivityService.updateStudentActivity(studentActivity));
    }

    /**
     * 删除综测情况
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:activity:remove')")
    @Log(title = "综测情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentActivityService.deleteStudentActivityByIds(ids));
    }
}
